Skip to content

SliceCheckableTreeProxyModel

Base classes

Name Children Inherits
SliceIdentityProxyModel
prettyqt.itemmodels.proxies.sliceidentityproxymodel
Base model for slice proxy models.

â‹” Inheritance diagram

graph TD
  3298527416["itemmodels.SliceCheckableTreeProxyModel"]
  4214724289["itemmodels.SliceIdentityProxyModel"]
  527136029["core.IdentityProxyModel"]
  2431222121["core.AbstractProxyModelMixin"]
  3882849576["core.AbstractItemModelMixin"]
  3227531497["core.ObjectMixin"]
  2751680898["builtins.object"]
  3668892142["QtCore.QIdentityProxyModel"]
  3989152870["QtCore.QAbstractProxyModel"]
  1898060022["QtCore.QAbstractItemModel"]
  3984060476["QtCore.QObject"]
  37984305["Shiboken.Object"]
  4214724289 --> 3298527416
  527136029 --> 4214724289
  2431222121 --> 527136029
  3882849576 --> 2431222121
  3227531497 --> 3882849576
  2751680898 --> 3227531497
  3668892142 --> 527136029
  3989152870 --> 3668892142
  1898060022 --> 3989152870
  3984060476 --> 1898060022
  37984305 --> 3984060476
  2751680898 --> 37984305

🛈 DocStrings

Bases: SliceIdentityProxyModel

Proxy model to make a tree model checkable.

In contrast to SliceCheckableProxyModel, any checkstate change is propagated to parent and child indexes. (child indexes will also get the new checkstate, parent indexes will become partially checked if needed.)

Example

model = MyModel()
table = widgets.TreeView()
table.set_model(model)
table.proxifier[:, 0].set_checkable(tree=True)
table.show()
# or
proxy = itemmodels.SliceCheckableTreeProxyModel(indexer=0)
proxy.set_source_model(model)
table.set_model(proxy)
table.show()
Source code in prettyqt/itemmodels/proxies/slicecheckableproxymodel.py
class SliceCheckableTreeProxyModel(itemmodels.SliceIdentityProxyModel):
    """Proxy model to make a tree model checkable.

    In contrast to SliceCheckableProxyModel, any checkstate change is propagated to
    parent and child indexes. (child indexes will also get the new checkstate, parent
    indexes will become partially checked if needed.)

    ### Example

    ```py
    model = MyModel()
    table = widgets.TreeView()
    table.set_model(model)
    table.proxifier[:, 0].set_checkable(tree=True)
    table.show()
    # or
    proxy = itemmodels.SliceCheckableTreeProxyModel(indexer=0)
    proxy.set_source_model(model)
    table.set_model(proxy)
    table.show()
    ```
    """

    ID = "checkable_tree"
    checkstate_changed = core.Signal(core.ModelIndex, constants.CheckState)

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self._checked: dict[tuple[int, int], constants.CheckState] = {}

    def flags(self, index: core.ModelIndex) -> constants.ItemFlag:
        if not index.isValid():
            return super().flags(index)
        if self.indexer_contains(index):
            # TODO: do i need to mess with ItemDataFlag.ItemIsTristate?
            return super().flags(index) | constants.IS_CHECKABLE
        return super().flags(index)

    def data(
        self,
        index: core.ModelIndex,
        role: constants.ItemDataRole = constants.DISPLAY_ROLE,
    ):
        if role == constants.CHECKSTATE_ROLE and self.indexer_contains(index):
            key = self.get_index_key(index, include_column=True)
            return self._checked.get(key, constants.CheckState.Unchecked)
        return super().data(index, role)

    def setData(
        self,
        index: core.ModelIndex,
        value: Any,
        role: constants.ItemDataRole = constants.EDIT_ROLE,
    ) -> bool:
        if role == constants.CHECKSTATE_ROLE and self.indexer_contains(index):
            state = (
                constants.CheckState.Checked
                if self.from_checkstate(value)
                else constants.CheckState.Unchecked
            )
            self.set_checkstate(index, state, recursive=True)
            return True
        return super().setData(index, value, role)

    def _set_state(self, index: core.ModelIndex, state: constants.CheckState):
        key = self.get_index_key(index, include_column=True)
        if self._checked.get(key, constants.CheckState.Unchecked) == state:
            return
        self._checked[key] = state
        self.dataChanged.emit(index, index, [constants.CHECKSTATE_ROLE])
        self.checkstate_changed.emit(index, state)

    def set_checkstate(
        self,
        index: core.ModelIndex,
        state: constants.CheckState | None = None,
        recursive: bool = False,
    ):
        """Set the checkstate of given index (toggle if state is None).

        If recursive is True, children get the same state and the parents are
        updated (partially checked if needed).
        """
        if state is None:
            key = self.get_index_key(index, include_column=True)
            current = self._checked.get(key, constants.CheckState.Unchecked)
            state = (
                constants.CheckState.Unchecked
                if current == constants.CheckState.Checked
                else constants.CheckState.Checked
            )
        self._set_state(index, state)
        if recursive:
            self.set_child_states(index, state)
            self.set_parent_states(index)
        return True

    def set_parent_states(self, index: core.ModelIndex):
        """Update checkstate of all ancestors based on their direct children."""
        column = index.column()
        parent = index.parent()
        while parent.isValid():
            parent = parent.sibling(parent.row(), column)
            children = [
                self.index(row, column, parent) for row in range(self.rowCount(parent))
            ]
            states = {self.data(child, constants.CHECKSTATE_ROLE) for child in children}
            if states == {constants.CheckState.Checked}:
                val = constants.CheckState.Checked
            elif states <= {constants.CheckState.Unchecked, None}:
                val = constants.CheckState.Unchecked
            else:
                val = constants.CheckState.PartiallyChecked
            self._set_state(parent, val)
            parent = parent.parent()

    def set_child_states(self, index: core.ModelIndex, state: constants.CheckState):
        """Set checkstate of all (already fetched) descendants."""
        column = index.column()
        for row in range(self.rowCount(index.siblingAtColumn(0))):
            child = self.index(row, column, index.siblingAtColumn(0))
            self._set_state(child, state)
            self.set_child_states(child, state)

set_checkstate(index: core.ModelIndex, state: constants.CheckState | None = None, recursive: bool = False)

Set the checkstate of given index (toggle if state is None).

If recursive is True, children get the same state and the parents are updated (partially checked if needed).

Source code in prettyqt/itemmodels/proxies/slicecheckableproxymodel.py
def set_checkstate(
    self,
    index: core.ModelIndex,
    state: constants.CheckState | None = None,
    recursive: bool = False,
):
    """Set the checkstate of given index (toggle if state is None).

    If recursive is True, children get the same state and the parents are
    updated (partially checked if needed).
    """
    if state is None:
        key = self.get_index_key(index, include_column=True)
        current = self._checked.get(key, constants.CheckState.Unchecked)
        state = (
            constants.CheckState.Unchecked
            if current == constants.CheckState.Checked
            else constants.CheckState.Checked
        )
    self._set_state(index, state)
    if recursive:
        self.set_child_states(index, state)
        self.set_parent_states(index)
    return True

set_child_states(index: core.ModelIndex, state: constants.CheckState)

Set checkstate of all (already fetched) descendants.

Source code in prettyqt/itemmodels/proxies/slicecheckableproxymodel.py
def set_child_states(self, index: core.ModelIndex, state: constants.CheckState):
    """Set checkstate of all (already fetched) descendants."""
    column = index.column()
    for row in range(self.rowCount(index.siblingAtColumn(0))):
        child = self.index(row, column, index.siblingAtColumn(0))
        self._set_state(child, state)
        self.set_child_states(child, state)

set_parent_states(index: core.ModelIndex)

Update checkstate of all ancestors based on their direct children.

Source code in prettyqt/itemmodels/proxies/slicecheckableproxymodel.py
def set_parent_states(self, index: core.ModelIndex):
    """Update checkstate of all ancestors based on their direct children."""
    column = index.column()
    parent = index.parent()
    while parent.isValid():
        parent = parent.sibling(parent.row(), column)
        children = [
            self.index(row, column, parent) for row in range(self.rowCount(parent))
        ]
        states = {self.data(child, constants.CHECKSTATE_ROLE) for child in children}
        if states == {constants.CheckState.Checked}:
            val = constants.CheckState.Checked
        elif states <= {constants.CheckState.Unchecked, None}:
            val = constants.CheckState.Unchecked
        else:
            val = constants.CheckState.PartiallyChecked
        self._set_state(parent, val)
        parent = parent.parent()

:fa-brands-github: Show source on GitHub